/*
 * @Author: gitee_com_zb
 * @Date: 2024-11-29 23:05:36
 * @LastEditors: gitee_com_zb
 * @LastEditTime: 2024-11-29 23:05:50
 * @FilePath: /algorithm/每日一题4.无重复字符的最长子串(medium).cpp
 * @Description: 题目链接 https://leetcode.cn/problems/longest-substring-without-repeating-characters
 */
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // 滑动窗口
        int hash[128] = {0}; // 模拟哈希表
        int n = s.size();
        int left = 0, right = 0, len = 0;
        while(right < n) {
            hash[s[right]]++; // 进窗口
            while(hash[s[right]] > 1) { // 判断
                hash[s[left]]--; // 出窗口
                left++;
            }
            len = max(len, right - left + 1); // 更新结果
            right++;
        }
        return len;
    }
};